import jwt
from rest_framework.response import Response
from django.conf import settings


def check_login(func):
    def wrapper(self,request,*args,**kwargs):
        token = request.headers.get('token')
        try:
            payload = jwt.decode(token,key=settings.SECRET_KEY,algorithms='HS256')
        except Exception as e:
            print(e)
            return Response({
                'code':403,
                'msg':'未认证'
            })
        request.user_id = payload.get('user_id')
        return func(self,request,*args,**kwargs)
    return wrapper
